// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DifferentialDriveStateUpdate.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Motion
{
    using System;

    using Microsoft.Robotics.Hardware.DAC;
    using Microsoft.Robotics.Navigation.Localization;    

    /// <summary>
    /// Maintains state about drive. State updates are triggered via DifferentialDriveMessage
    /// </summary>
    public class DifferentialDriveStateUpdate
    {
        /// <summary>
        /// Wheel calibration parameters.
        /// </summary>
        private DifferentialWheelCalibration wheelCalibration;

        /// <summary>
        /// A set of errors and standard deviations to apply to motion
        /// </summary>
        private OdometryNoiseModelParams noiseParams;

        /// <summary>
        /// Previous drive feedback state;
        /// </summary>
        private DriveFeedbackState previousDriveFeedbackState;

        /// <summary>
        /// Initializes a new instance of the <see cref="DifferentialDriveStateUpdate"/> class
        /// </summary>
        /// <param name="wheelCalibration">Wheel calibration parameters</param>
        /// <param name="noiseParams">Odometry noise model parameters</param>
        public DifferentialDriveStateUpdate(DifferentialWheelCalibration wheelCalibration, OdometryNoiseModelParams noiseParams)
        {
            this.wheelCalibration = wheelCalibration;
            this.noiseParams = noiseParams;
            this.previousDriveFeedbackState = null;
        }

        /// <summary>
        /// Update the DriveFeedbackState state (left by adding increments from a 
        /// DifferentialDriveState state.
        /// </summary>
        /// <param name="differentialDriveState">Incoming differential drive state</param>
        /// <returns>updated drive state</returns>
        public DriveFeedbackState UpdateDriveFeedback(DifferentialDriveState differentialDriveState)
        {
                if (differentialDriveState.WheelBaseInMeters <= 0.0)
                {
                    throw new ArgumentOutOfRangeException(string.Format("WheelBase width value when initializing state updates is invalid {0}", differentialDriveState.WheelBaseInMeters));
                }

            double leftWheelMean = differentialDriveState.LeftWheelPositionInMeters * this.wheelCalibration.WheelAverageDiameterScalingFactor;
            double rightWheelMean = differentialDriveState.RightWheelPositionInMeters * this.wheelCalibration.WheelAverageDiameterScalingFactor;
            double wheelBase = differentialDriveState.WheelBaseInMeters * this.wheelCalibration.WheelBaseScalingFactor;
            DriveFeedbackState driveFeedbackState = new DriveFeedbackState(
                leftWheelMean, 
                rightWheelMean,                
                wheelBase,
                this.noiseParams,
                this.previousDriveFeedbackState);
            this.previousDriveFeedbackState = driveFeedbackState;
            return driveFeedbackState;
        }
    }
}
